package action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;

import entity.Answer;
import entity.Choose;
import entity.Question;
import entity.Survey;
import entity.User;
import mapper.UserMapper;
import service.UserService;

@Controller
@SessionAttributes({ "userId", "passWord" })

public class UserAction {
	@Autowired
	private UserService userSeriver;

	public UserService getUserSeriver() {
		return userSeriver;
	}

	public void setUserSeriver(UserService userSeriver) {
		this.userSeriver = userSeriver;
	}

	@RequestMapping("/login.do")

	public ModelAndView Login(User user, Map<String, Object> model) {

		model = new HashMap<String, Object>();

		if (userSeriver.Login(user) != null) {
			model.put("userId", user.userId);
			return new ModelAndView("home", model);
		} else {
			model.put("error", "Logon failed");
			return new ModelAndView("login", model);
		}

	}

	// 注销当前用户
	@RequestMapping("/toLogin")
	public String execute(HttpSession session, SessionStatus sessionStatus) {
		session.removeAttribute("userName");
		session.removeAttribute("userPwd");
		session.invalidate();
		// 使用SessionAttributes后使用才能完全消除Session
		sessionStatus.setComplete();
		return "redirect:toLogin.jsp";
	}

	@RequestMapping("/register")
	public ModelAndView Register(String userId, String passWord) {
		User user = new User();
		user.setUserId(userId);
		user.setPassWord(passWord);
		userSeriver.Register(user);
		return new ModelAndView("toRegister");
	}

	@RequestMapping("/querySurvey")
	public ModelAndView querySurvey(Integer queryId) {
		Survey survey = userSeriver.querySurvey(queryId);
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("survey", survey);
		return new ModelAndView("SurveyList", model);
	}

	@RequestMapping("/answerSurvey")
	public @ResponseBody String answerSurvey(Integer surveyId) {
		List<Question> questionList = new ArrayList<Question>();
		List<Choose> chooseList = new ArrayList<Choose>();
		Question[] questionNum = null;
		Choose[] chooseNum = null;
		// 查询问题详情
		questionNum = userSeriver.queryQuestion(1);
		// 创建问题个数数组用来保存每个问题的选项个数
		Integer[] chooseLen = new Integer[questionNum.length];
		for (int i = 0; i < questionNum.length; i++) {
			// 获得选项个数
			chooseLen[i] = userSeriver.countChoose(questionNum[i].getQuestionId());
			// 把获得的问题添加到问题数列
			questionList.add(questionNum[i]);

			for (int j = 0; j < chooseLen[i]; j++) {
				// 获得选项详情
				chooseNum = userSeriver.queryChoose(questionNum[i].getQuestionId());
			}
			for (int k = 0; k < chooseLen[i]; k++) {
				// 添加选项数列
				chooseList.add(chooseNum[k]);
			}
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("question", questionList);
		map.put("choose", chooseList);
		Gson gson = new Gson();
		String result = gson.toJson(map);
		return result;
	}

	@RequestMapping("/addAnswer")
	public String addAnswer(Integer[] choose,Integer[] questionId,Integer surveyId,String userId){
		int total = 0;
		List<Answer> answerList = new ArrayList<Answer>();
		Integer[] nums = new Integer[questionId.length];
		//记录问题的选项个数,String userId
//		System.out.println(questionId.length);
		for(int i = 0; i < questionId.length; i++){
			nums[i] = userSeriver.countChoose(questionId[i]);
			for(int j = 0; j < nums[i]; j++){
				Answer answer = new Answer();
				answer.setAnswer(choose[j + total]);
				answer.setSurveyId(surveyId);
				answer.setUserId(userId);
				answer.setChooseId(questionId[i]);
				answerList.add(answer);
			}
			total += nums[i]; 
		}
		boolean suChoose = false;
		for(int i = 0; i < answerList.size(); i++){
			suChoose = userSeriver.addAnswer(answerList.get(i));
		}		
			return "home";
		
	}
}